﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class administration_ejemplo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack){
            
            intPageSize.Text = "3"; //aqui indican el tamañano de la página            
            intCurrIndex.Text = "0";
            Bind();
        }
    }

    private void Bind()
    {
        //SqlConnection objConn = new SqlConnection("Data Source=JAIME-PC;Initial Catalog=dbUniversal;Integrated Security=True");
        //SqlDataAdapter objDA = new SqlDataAdapter("SELECT * FROM tblPNatural", objConn);
        DataSet objDS = new DataSet();
        clsPNatural objPNatural = new clsPNatural();
        

        if(!Page.IsPostBack){

            //objDA.Fill(objDS);
            objDS = objPNatural.SeeAllTheRecords();
            intRecordCount.Text = objDS.Tables[0].Rows.Count + "";
            objDS = null;
            objDS = new DataSet();
        }
        
        if(intCurrIndex.Text.Equals("0")){
            hrefPrevious.Visible = false;
        }
        else{
            hrefPrevious.Visible = true;
        }
        
        //objDA.Fill(objDS, Int32.Parse(intCurrIndex.Text), Int32.Parse(intPageSize.Text), "Logs");
        objDS = objPNatural.SeeAllTheRecords(Int32.Parse(intCurrIndex.Text), Int32.Parse(intPageSize.Text),"");

        dList.DataSource = objDS.Tables[0].DefaultView;
        dList.DataBind();
        //objConn.Close();
        PrintStatus();
    }

    public void ShowFirst(object sender, EventArgs e)
    {
        intCurrIndex.Text = "0";
        Bind();
    }

    public void ShowPrevious(object sender, EventArgs e)
    {
        intCurrIndex.Text = (Int32.Parse(intCurrIndex.Text) - Int32.Parse(intPageSize.Text)) + "";
        if(Int32.Parse(intCurrIndex.Text)<0){
            intCurrIndex.Text = "0";
        }
        Bind();
    }

    public void ShowNext(object sender, EventArgs e)
    {
        if (Int32.Parse(intCurrIndex.Text) + 1 < Int32.Parse(intRecordCount.Text)){
            intCurrIndex.Text = (Int32.Parse(intCurrIndex.Text) + Int32.Parse(intPageSize.Text)) + "";
        }
        Bind();
    }

    public void ShowLast(object sender, EventArgs e)
    {
        int tmpInt = 0;

        tmpInt = Int32.Parse(intRecordCount.Text) % Int32.Parse(intPageSize.Text);
        if (tmpInt > 0){
            intCurrIndex.Text = (Int32.Parse(intRecordCount.Text) - tmpInt) + "";
        }
        else{
            intCurrIndex.Text = (Int32.Parse(intRecordCount.Text) - Int32.Parse(intPageSize.Text)) + "";
        }
        Bind();
    }

    private void PrintStatus(){
        lblStatus.Text = "Total Records:<b>" + intRecordCount.Text;
        lblStatus.Text += "</b> - Showing Page:<b> ";
        lblStatus.Text += ((Int32.Parse(intCurrIndex.Text)) / (Int32.Parse(intPageSize.Text) + 1)) + "";
        lblStatus.Text += "</b> of <b>";

        if((Int32.Parse(intRecordCount.Text) % Int32.Parse(intPageSize.Text)) > 0){
            lblStatus.Text += ((Int32.Parse(intRecordCount.Text) / (Int32.Parse(intPageSize.Text) + 1))) + "";
        }
        else{
            lblStatus.Text += (Int32.Parse(intRecordCount.Text) / Int32.Parse(intPageSize.Text)) + "";
        }
        lblStatus.Text += "</b>";
    }

    
}
